home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / localloginpw / lin.c < prev    next >
C/C++ Source or Header  |  1995-11-05  |  8KB  |  294 lines

  1. //---------------------------------------------------------------------------
  2. // LOGIN                                        (c) Laurence Vanhelsuwe 1994
  3. // -----
  4. // This program is part of a group of 3 complementary programs to log the
  5. // usage of a stand-alone machine. (LOGIN, LOGOUT, PASSWORD)
  6. //
  7. // This program has been written in ANSI C to make it fully portable across
  8. // different hardware platforms.
  9. //
  10. // Original development and testing was carried out on a Commodore Amiga 3000.
  11. //
  12. // This program and its companion LOGOUT will maintain a log file detailing
  13. // machine usage for all authorised users.
  14. // Authorised users are added by the PASSWORD command, which itself can only
  15. // be used by a 'System Operator' who knows the password to activate the
  16. // program.
  17. //
  18. // 2 Datafiles are maintained by the trio of programs:
  19. //  - USERLOG.DAT
  20. //  - PASSWOR.DAT
  21. //
  22. // History
  23. // -------
  24. // 01-JAN-94: Design and initial file creation.
  25. // 13-JAN-94: ANSI C portability MY FOOT !!! Still struggling with porting this!
  26. // 26-JAN-94: Finally got it: ANSI level 1 defines binary and text modes.
  27. //              I was using TEXT mode... which interprets EOF differently !!
  28. // 07-FEB-94: Disabled Control-C checking for Amiga implementation
  29. //
  30. //---------------------------------------------------------------------------
  31.  
  32. //#define        DEBUG            1
  33.  
  34. #include    "stdio.h"
  35. #include    "stdlib.h"
  36. #include    "string.h"
  37. #include    "fcntl.h"
  38. #include    "time.h"
  39.  
  40. #define        TRUE            (1)
  41. #define        FALSE            (0)
  42.  
  43. #define        MAX_NAME_LEN    (10)
  44. #define        ENTRY_LEN        (65)
  45.  
  46. #ifdef    AMIGA
  47. #define        LFILE            "DEVS:USERLOG.DAT"
  48. #define        PFILE            "DEVS:PASSWORDS.DAT"
  49. #define        HELP            "?"
  50. #define        SET_FG_0        ""
  51. #define        SET_FG_1        ""
  52. #define        CREAT_MODE        (O_RDWR)
  53. #define        OPEN_MODE        (O_RDWR)
  54. void __regargs __chkabort(void);
  55.  
  56. #else
  57.  
  58. #include    "sys\stat.h"
  59. #define        LFILE            "C:/DOS/USERLOG.DAT"
  60. #define        PFILE            "C:/DOS/PASSWOR.DAT"
  61. #define        HELP            "/?"
  62. #define        SET_FG_0        ""
  63. #define        SET_FG_1        ""
  64. #define        CREAT_MODE        (S_IREAD | S_IWRITE)
  65. #define        OPEN_MODE        (O_RDWR | O_BINARY)
  66.  
  67. #endif
  68.  
  69. //---------------------------------------------------------------------------
  70. // Function Prototypes
  71. // -------------------
  72. //---------------------------------------------------------------------------
  73.  
  74. typedef        char    BOOL;
  75.  
  76. void get_log_entry(void);
  77. void add_log_entry(void);
  78. void decode_n_term(char *ptr, int offset);
  79.  
  80. BOOL valid_user(void);
  81.  
  82. //---------------------------------------------------------------------------
  83. // Globals
  84. // -------
  85. //---------------------------------------------------------------------------
  86.  
  87. char entry_buffer[]="X AAAAAAAAAA DDD MMM dd hh:mm:ss YYYY - XXX XXX XX XX:XX:XX XXXX\r";
  88.  
  89. int logfile, passwfile;            // LEVEL 1 ANSI File Handles
  90.  
  91. char username[80];                // input buffers from keyboard
  92. char userpassword[80];
  93.  
  94. long int file_size, log_pos;
  95. int num_entries, attempt;
  96.  
  97. time_t    systime;
  98. struct tm *tim;
  99.  
  100. //---------------------------------------------------------------------------
  101. //---------------------------------------------------------------------------
  102.  
  103. main(int argc, char **argv) {
  104.  
  105. BOOL logged_out;
  106.  
  107.     // If there's anything on the command line...
  108.  
  109.     if(argc > 1 ) {
  110.         printf("LOGIN V1.0  (Copyright (c) Jan 1994 L. Vanhelsuwe)\n\n");
  111.         printf("Programmed by Laurence Vanhelsuwe in C on an Amiga 3000.\n");
  112.         printf("Ported to IBM PC environment by L. Vanhelsuwe.\n\n");
  113.         printf("USAGE: LOGIN [%s]\n\n", HELP);
  114.     }
  115.  
  116.     if ((passwfile = open(PFILE, OPEN_MODE)) == -1) {
  117.         fprintf(stderr, "Passwords file does not exist ! (Use PASS to create)\n");
  118.         exit(10);
  119.     }
  120.  
  121.     printf("\n\n");
  122.     printf("====================================================\n\n");
  123.     printf("                  PLEASE LOG ON\n\n");
  124.     printf("====================================================\n\n");
  125.  
  126.  
  127.     logged_out = TRUE;
  128.     if ((  logfile = open(LFILE, OPEN_MODE)) != -1) {
  129.  
  130.         get_log_entry();                    // get last log entry in entry_buffer
  131.  
  132.         if (entry_buffer[0] == 'I') {
  133.             logged_out = FALSE;
  134.             decode_n_term(&entry_buffer[2], 0);        // just 0-terminate
  135.             printf("   --WARNING--\n\n'%s' didn't log out !\n\n", &entry_buffer[2]);
  136.         }
  137.     } else {
  138.         printf("Log file doesn't exist. Attempting to create it...\n");
  139.         logfile = creat(LFILE, CREAT_MODE);
  140.         if (logfile == -1) {
  141.             close(passwfile);
  142.             fprintf(stderr, "Failed to create log file... aborting !\n");
  143.             exit(10);
  144.         }
  145.         printf("Log file created OK.\n");
  146.     }
  147.  
  148. // Get users name and password and validate entries.
  149. // A special case is if 
  150.  
  151.     for (attempt=0; attempt < 3; attempt++) {
  152.         do {
  153.             printf("Please enter your name:");
  154.             scanf("%s", username);
  155.  
  156.             if (( ! logged_out) && (strcmp(username, "CONTINUE")==0)) {
  157.                 close(passwfile);
  158.                 printf("\nUser '%s': Continued Session Log-On.\n\n", &entry_buffer[2]);
  159.                 return 0;
  160.             }
  161.  
  162.         } while (strlen(username) > MAX_NAME_LEN);
  163.         
  164.         do {
  165.             printf(SET_FG_1); printf("Please enter your password:");
  166.             printf(SET_FG_0); scanf("%s", userpassword);
  167.  
  168.         } while (strlen(userpassword) > MAX_NAME_LEN);
  169.  
  170.         printf(SET_FG_1);
  171.  
  172.         if (valid_user()) break;
  173.  
  174.         printf("\nLogin attempt refused... try again.\n\n");
  175.     }
  176.  
  177.     close(passwfile);
  178.  
  179.     if (attempt==3) {
  180.         printf("Number of Login attempts exceeded.\n\n");
  181.         printf("  -- MACHINE HALTED --");
  182.         while (attempt) ;                        // LOOP forever
  183.     }
  184.  
  185.     entry_buffer[0]  = 'I';            // set entry to correctly logged IN
  186.     
  187.     strncpy(&entry_buffer[2], "                              ", MAX_NAME_LEN+1);
  188.     strncpy(&entry_buffer[2], username, strlen(username));
  189.     
  190.     time(&systime);                        // get machine local time
  191.     tim = localtime(&systime);            // convert to universal time format
  192.     strncpy(&entry_buffer[40], asctime(tim), 24);
  193.     strncpy(&entry_buffer[13], asctime(tim), 24);
  194.     
  195.     add_log_entry();
  196.     printf("\nUser '%s' Logged on.\n\n", username);
  197.  
  198.     return 0;
  199. }
  200.  
  201. //---------------------------------------------------------------------------
  202. //---------------------------------------------------------------------------
  203. void get_log_entry(void) {
  204. int chars;
  205.  
  206.     lseek(logfile, 0L, SEEK_END); file_size = tell(logfile);
  207.  
  208.     num_entries = file_size/ENTRY_LEN;
  209.     log_pos = (num_entries-1)*ENTRY_LEN;
  210.  
  211. #ifdef DEBUG
  212.     printf("File size = %ld, Num Entries = %d, LOG Position = %d\n",
  213.              file_size, num_entries, log_pos);
  214. #endif
  215.  
  216.     lseek(logfile, log_pos, SEEK_SET);
  217.     chars = read (logfile, (void*) entry_buffer, ENTRY_LEN);
  218.  
  219. #ifdef DEBUG
  220.     printf("Entry buffer[0] = '%c'\n", entry_buffer[0]);
  221.     printf("Entry buffer[1] = '%c'\n", entry_buffer[1]);
  222.     printf("Entry buffer[2] = '%c'\n", entry_buffer[2]);
  223.     printf("Entry buffer[3] = '%c'\n", entry_buffer[3]);
  224.  
  225.     if (chars != ENTRY_LEN) {
  226.         printf("ERROR: get_log_entry() read didn't read %d chars (but %d!)\n", ENTRY_LEN, chars);
  227.         exit(10);
  228.     }
  229.  
  230. #endif
  231.  
  232.     entry_buffer[12] = 0;            // zero-terminate user name field
  233. }
  234. //---------------------------------------------------------------------------
  235. void add_log_entry(void) {
  236.  
  237.     lseek(logfile, 0L, SEEK_END);
  238.     write(logfile, (void*) entry_buffer, ENTRY_LEN);
  239.     close(logfile);
  240. }
  241. //---------------------------------------------------------------------------
  242. //---------------------------------------------------------------------------
  243. BOOL valid_user(void) {
  244.  
  245. char name[MAX_NAME_LEN+2];
  246. char pasw[MAX_NAME_LEN+2];
  247. char user_entry_buf[(MAX_NAME_LEN*2)+1];
  248. int bytes_read;
  249.  
  250.     lseek(passwfile, 0L, SEEK_SET);            // rewind passwords file.
  251.  
  252.     bytes_read = read(passwfile, user_entry_buf, (MAX_NAME_LEN*2)+1);
  253.     do {
  254. #ifdef    DEBUG
  255.         printf("Bytes read from passwfile = %d\n", bytes_read);
  256.  
  257.         for (i=0 ; i<21; i++) {
  258.         printf("PASSWLINE[%d] = '%c'\n", i , user_entry_buf[i]); }
  259. #endif
  260.  
  261.         name[MAX_NAME_LEN] = pasw[MAX_NAME_LEN] = ' ';
  262.  
  263.         strncpy(name,  user_entry_buf              , MAX_NAME_LEN);
  264.         strncpy(pasw, &user_entry_buf[MAX_NAME_LEN], MAX_NAME_LEN);
  265.  
  266.         decode_n_term(name, -1); decode_n_term(pasw, -2);
  267.  
  268.         if ((strcmp(username,      name))==0 &&
  269.             (strcmp(userpassword, pasw))==0 )    return TRUE;
  270.  
  271.         bytes_read = read(passwfile, user_entry_buf, (MAX_NAME_LEN*2)+1);
  272.  
  273.     } while (bytes_read);
  274.  
  275.     return FALSE;
  276. }
  277. //---------------------------------------------------------------------------
  278. //---------------------------------------------------------------------------
  279. void decode_n_term(char *ptr, int offset) {
  280.  
  281.     while(*ptr != ' ') {
  282.         *ptr = (*ptr) + offset;
  283.         ptr++;
  284.     }
  285.  
  286.     *ptr = 0;
  287. }
  288. //---------------------------------------------------------------------------
  289. // Disable CTRL-C checking.
  290. //---------------------------------------------------------------------------
  291. #ifdef AMIGA
  292. void __regargs __chkabort(void) {}
  293. #endif
  294.